const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
		':');
};

const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : '0' + n;
};

const getHoursInfo = () => {
	var hour = [];
	const crrHour = new Date().getHours();

	switch (crrHour) {
		case 9:
		case 10:
		case 11:
		case 12:
			hour = [{
					name: '12:00-15:00',
					start: [12, 0],
					end: [15, 0]
				},
				{
					name: '12:30-15:30',
					start: [12, 30],
					end: [15, 30]
				},
				{
					name: '13:00-16:00',
					start: [13, 0],
					end: [16, 0]
				},
				{
					name: '13:30-16:30',
					start: [13, 30],
					end: [16, 30]
				},
				{
					name: '14:00-17:00',
					start: [14, 0],
					end: [17, 0]
				},
				{
					name: '14:30-17:30',
					start: [14, 30],
					end: [17, 30]
				},
				{
					name: '15:00-18:00',
					start: [15, 0],
					end: [18, 0]
				}
			];
			break;

		case 13:
			hour = [{
					name: '13:00-16:00',
					start: [13, 0],
					end: [16, 0]
				},
				{
					name: '13:30-16:30',
					start: [13, 30],
					end: [16, 30]
				},
				{
					name: '14:00-17:00',
					start: [14, 0],
					end: [17, 0]
				},
				{
					name: '14:30-17:30',
					start: [14, 30],
					end: [17, 30]
				},
				{
					name: '15:00-18:00',
					start: [15, 0],
					end: [18, 0]
				}
			];
			break;

		case 14:
			hour = [{
					name: '14:00-17:00',
					start: [14, 0],
					end: [17, 0]
				},
				{
					name: '14:30-17:30',
					start: [14, 30],
					end: [17, 30]
				},
				{
					name: '15:00-18:00',
					start: [15, 0],
					end: [18, 0]
				}
			];
			break;

		case 15:
		case 16:
		case 17:
			hour = [{
				name: '15:00-18:00',
				start: [15, 0],
				end: [18, 0]
			}];
			break;
	}

	return hour;
};

const damageQuantity = (serviceUrl, that) => {
	uni.request({
		url: serviceUrl + '/selQueryDamageQuantity',
		data: {
			key: uni.getStorageSync('userKey')
		},
		header: {
			'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
		},

		success(res) {
			const data = res.data;

			if (data.data) {
				uni.showModal({
					title: '提示',
					content: '您的上一个订单有尚未支付的费用',
					confirmText: '查看详情',
					cancelText: '去支付',

					success(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '../paymentForDamage/index'
							});
						} else {
							uni.request({
								url: serviceUrl + '/PaymentDamage',
								data: {
									key: uni.getStorageSync('userKey')
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
								},

								success(res) {
									const info = res.data;

									if (info.state == 1) {
										that.$payment(info.data);
									} else {
										uni.showToast({
											title: info.notice,
											icon: 'none',
											duration: 3000
										});
									}
								}
							});
						}
					}
				});
			}
		}
	});
	uni.request({
		url: serviceUrl + '/overtimeUnpaid',
		data: {
			key: uni.getStorageSync('userKey')
		},
		header: {
			'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
		},

		success(res) {
			uni.hideToast();
			const data = res.data;

			if (data.state == 1) {
				var info = data.data;
				uni.showModal({
					title: '提示',
					content: '您的上一个订单还有尚未支付的超时费用，请在完成支付后再试。超时费总额' +
						info.Price / 100 +
						'元，超时费=房费×' +
						info.Percentage +
						'%×超时小时数（超时时长不足一小时按一小时计算）',
					confirmText: '去支付',

					success(res) {
						if (res.confirm) {
							that.$payment(info.PaymentData);
						}
					}
				});
			}
		}
	});
}; //object转string,用于签名计算

let obj2str = function(args) {
	var keys = Object.keys(args);
	keys = keys.sort(); //参数名ASCII码从小到大排序（字典序）；

	var newArgs = {};
	keys.forEach(function(key) {
		if (args[key] != '' && args[key] != 'undefined') {
			//如果参数的值为空不参与签名；
			newArgs[key] = args[key]; //参数名区分大小写；
		}
	});
	var string = '';

	for (var k in newArgs) {
		string += k + newArgs[k];
	}

	return string;
};

module.exports = {
	formatTime: formatTime,
	getHours: getHoursInfo,
	damageQuantity: damageQuantity,
	obj2str: obj2str
};
